﻿using LVYAN.Model;
using LVYAN.Model.AutoMation;
using LVYAN.Service.AIAPI;
using LVYAN.Utils;
using System.Collections.Generic;
using System.Linq;
namespace LVYAN.Service
{
    public class QWenService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public ResponseMessage Generation(RobotRobotOther Robot, string MouldeKey, List<MessagesItem> list)
        {
            new LoggerService().ResponseMsgDebug("千文模型", "发送消息", list);

            QWenRequestGeneration wenGeneration = new QWenRequestGeneration
            {
                input = new Input
                {
                    messages = list,
                },
                model = "qwen-max",
                parameters = new Parameters { result_format = "message" }
            };
            var content = JsonService.ToString(wenGeneration);
            var result = new RequestService().PostJson("https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation", content,new List<(string, string)> { ("Authorization", $"Bearer {MouldeKey}") });
            if (string.IsNullOrWhiteSpace(result))
            {
                return new ResponseMessage { Success = false, };
            }
            new LoggerService().ResponseMsgDebug("千文模型", "接收消息", result);
            var resultModel = JsonService.ToModel<QWenResponseGeneration>(result);
            var response= new ResponseMessage { Success = true, Message = resultModel.output.choices[0].message.content, Role = resultModel.output.choices[0].message.role, modelid = "qwen-max", InputToken = resultModel.usage.input_tokens, OutToken = resultModel.usage.input_tokens, };
            MoudleManage.ModuleRecord(Robot, list.LastOrDefault().content, content, result, response.InputToken ?? 0, response.OutToken ?? 0);
            return response;
        }

    }
}
